package com.zhang.learn.mapstruct;

import cn.hutool.core.date.DateUtil;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author zhanghaolian
 * @date 2021/6/6 23:42
 */
@Mapper
//@MapperConfig
public abstract class Car2CarDtoMapper {
    static Car2CarDtoMapper INSTANCE= Mappers.getMapper(Car2CarDtoMapper.class);

    // 不同的属性名映射
    @Mapping(source = "numberOfSeats", target = "seatCount")
    // 数字格式化
    @Mapping(source = "price", target = "price", numberFormat = "#.00")
    // 忽略某个字段的映射
    @Mapping(target = "color", ignore = true)
    // 时间格式化
    @Mapping(source = "productDate", target = "productDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    // 属性是引用对象的处理
    @Mapping(source = "driver",target = "driverDto")
    // 设置默认值
    @Mapping(source = "saleStatus",target = "saleStatus",defaultValue = "1")
    // java表达式使用
    @Mapping(target = "outFactoryDate" ,expression = "java(new java.util.Date())")
    @Named("carToCarDto")
    public abstract CarDto carToCarDto(Car car);


    @Mapping(source = "seatCount",target = "numberOfSeats")
    public abstract Car CarDtoToCar(CarDto dto);

    /**
     * Driver --> DriverDto 转换
     * @param driver
     * @return
     */
    @Mapping(source = "id", target = "driverId")
    @Mapping(source = "name",target = "driverName")
    public abstract DriverDto driverToDriverDto(Driver driver);

    /**
     * 最后一步对属性的自定义描述处理
     * @param car
     * @param carDto
     */
    @AfterMapping
    public void CarToCarDtoAfter(Car car,@MappingTarget CarDto carDto) {
        System.out.println("carDto.getName() = " + carDto.getName());
        System.out.println("**************************************");
    }

    /**
     * 集合转化
     * @param cars
     * @return
     */
    @InheritConfiguration(name = "carToCarDto")
    public abstract List<CarDto> carToCarDtos(List<Car> cars);


    /**
     * @BeanMapping(ignoreByDefault = true) 表示忽略默认的映射规则
     * 只有自己写的@Mapping注解的映射规则生效
     *
     * @param car
     * @return
     */
    @BeanMapping(ignoreByDefault = true)
    @InheritConfiguration(name = "carToCarDto")
    @Named("carToCarDtoInherit")
    public abstract CarDto carToCarDtoInherit(Car car);



}
